//
//  YCCommentsTableViewCell.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/14.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCCommentsTableViewCell.h"
#import "YCCommentModel.h"

@interface YCCommentsTableViewCell()<TTTAttributedLabelDelegate>

@property (nonatomic,readonly) TTTAttributedLabel *label;
@property (nonatomic,readonly) YCCommentModel *model;

@end

@implementation YCCommentsTableViewCell

@synthesize label = _label;
@synthesize model = _model;


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        _label.textAlignment = NSTextAlignmentLeft;
        _label.textColor = [UIColor darkGrayColor];
        _label.font = [UIFont systemFontOfSize:13];
        _label.numberOfLines = 0;
        _label.delegate = self;
        _label.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
        _label.lineBreakMode = NSLineBreakByCharWrapping;
        _label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
        [self.contentView addSubview:self.label];
        
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@5);
            make.right.equalTo(@(-5));
            make.top.equalTo(@0);
        }];
        
    }
    return self;
}

- (void)relodCommentsWithModel:(YCCommentModel *)model
{
    _model = model;
    
    NSString *contentString = @"";
    contentString = [NSString stringWithFormat:@"%@: %@",model.uname,model.content];
//    if ([model.level isEqualToString:@"1"])
//    {
//        contentString = [NSString stringWithFormat:@"%@: %@",model.fromUserNickname,model.commentContent];
//    }
//    else
//    {
//        contentString = [NSString stringWithFormat:@"%@ 回复 %@: %@",model.fromUserNickname,model.toUserNickname,model.commentContent];
//    }
    
    [self.label setText:contentString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        
        [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor yc_colorWithUInt:0x00b4ff] CGColor] range:[contentString rangeOfString:model.uname]];
        
//        if ([model.level isEqualToString:@"2"])
//        {
//            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor yc_colorWithUInt:0x00b4ff] CGColor] range:[contentString rangeOfString:model.uname]];
//        }
        
        return mutableAttributedString;
    }];
    
    [self.label addLinkToPhoneNumber:@"1" withRange:[contentString rangeOfString:model.uname]];
    
//    if ([model.level isEqualToString:@"2"])
//    {
//        [self.label addLinkToPhoneNumber:@"2" withRange:[contentString rangeOfString:model.uname]];
//    }
}

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
    YCLog(@"点击了评论中用户的名字:%@",self.model.uname);
    if ([self.delegate respondsToSelector:@selector(didTouchUpInsideWithUserName:)]) {
        [self.delegate didTouchUpInsideWithUserName:self.model];
    }
//    if([self.delegate respondsToSelector:@selector(didTouchUpInsideWithUserName:)])
//    {
//        GOShowOffModel *showOffModel = [[GOShowOffModel alloc]init];
//        
//        if ([phoneNumber isEqualToString:@"1"])
//        {
//            showOffModel.userId = _model.fromUserId;
//            showOffModel.nickName = _model.fromUserNickname;
//            showOffModel.headPhoto = _model.fromUserHeadPhoto;
//        }
//        else
//        {
//            showOffModel.userId = _model.toUserId;
//            showOffModel.nickName = _model.toUserNickname;
//            showOffModel.headPhoto = _model.toUserHeadPhoto;
//        }
//        
//        [self.delegate didTouchUpInsideWithUserName:showOffModel];
//    }
}
//


@end
